package com.runen.wnhz.runen.ui.activity.major;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.ruffian.library.RTextView;
import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.MessageDetailBean;
import com.runen.wnhz.runen.common.utils.ACacheUtlis;
import com.runen.wnhz.runen.common.utils.NetUtils;
import com.runen.wnhz.runen.common.utils.OkHttpUtils;
import com.runen.wnhz.runen.common.utils.StatusBarCompatUtils;
import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.UserBean;
import com.runen.wnhz.runen.di.component.ApplicationComponent;
import com.runen.wnhz.runen.ui.activity.BaseActivity;
import com.runen.wnhz.runen.widget.TitleBuilder;

import java.io.IOException;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class MessageDetailActivity extends BaseActivity {

    @BindView(R.id.image_message_label)
    ImageView imageMessageLabel;
    @BindView(R.id.rtext_message_content)
    RTextView rtextMessageContent;
    private String id;
    private String token;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.message_detail_activity;
    }

    @Override
    protected void onTitleClickListen(TitleBuilder.TitleButton clicked) {
        switch (clicked) {
            case LEFT:
                Intent intent = new Intent();
                intent.putExtra("is_readed",true);
                setResult(Activity.RESULT_OK,intent);
                finish();
                break;
            case MIDDLE:
                break;
            case RIGHT:
                break;
            default:
                break;
        }
    }

    private void getSystemMessageDetail(){
        if(token != null && id != null){
            FormBody formBody = new FormBody.Builder().add("token",token).add("id",id).build();
            Request request = new Request.Builder().url(NetUtils.USER_MESSAGE_DETAIL_SYSTEM).post(formBody).build();
            Call newCall = OkHttpUtils.getOkHttpClient().newCall(request);
            newCall.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           ToastUtil.showToast("数据请求失败");
                       }
                   });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    try {
                        handleMessageDetailData(response);
                    } catch (IOException e) {
                        e.printStackTrace();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.showToast("数据请求出现异常");
                            }
                        });
                    }
                }
            });
        }
    }

    private void handleMessageDetailData(Response response) throws IOException{
        if(response.isSuccessful()){
            ResponseBody responseBody = response.body();
            if(responseBody != null) {
                final String json = responseBody.string();
                System.out.println("+++++++++++ == " + json);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MessageDetailBean messageDetailBean = new Gson().fromJson(json, MessageDetailBean.class);
                        if("1".equals(messageDetailBean.getRe())){
                            MessageDetailBean.DataBean dataBean = messageDetailBean.getData();
                            if(dataBean != null){
                                String describe = dataBean.getDescribe();
                                if(describe != null){
                                    rtextMessageContent.setText(describe);
                                }
                            }
                        }
                    }
                });
            }
        }

    }

    @Override
    protected void initStart() {
        StatusBarCompatUtils.getInstance().TranslucentStatusBar(this, R.color.title_green);
        id = getIntent().getStringExtra("id");
        ACacheUtlis cacheUtlis = ACacheUtlis.getInstance();
        if(cacheUtlis != null){
            UserBean reqeustUser = cacheUtlis.getReqeustUser(this);
            if(reqeustUser != null){
                token = reqeustUser.getToken();
            }
        }
        getSystemMessageDetail();
    }

    @Override
    public void initToolBar(TitleBuilder mTitleBuilder) {
        mTitleBuilder.setMiddleTitleText("消息详情")
                .setLeftDrawable(R.mipmap.icon_back)
                .seTitleBgRes(R.mipmap.bg_title)
                .setMiddleTitleTextColor(ContextCompat.getColor(this, R.color.text_white));
    }

    @Override
    public void setupAcitivtyComponent(ApplicationComponent appComponent) {

    }
}
